package me.ingxin.android.sample.lego

import android.app.Activity
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityOptionsCompat
import me.ingxin.android.router.Router
import me.ingxin.android.router.route.Result
import me.ingxin.android.sample.lego.databinding.ActivityMainBinding
import me.ingxin.android.testlibrary.any.TestService

class MainActivity : AppCompatActivity() {

    private val binding by lazy { ActivityMainBinding.inflate(layoutInflater) }

    private var testService: TestService? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        binding.act1.setOnClickListener {
            //main thread
            val opts = ActivityOptionsCompat.makeSceneTransitionAnimation(
                this,
                binding.act1,
                "share_view1"
            )
            //work thread
            Thread{
                Router
                    .build("/activity/test1?a=我是从uri上携带的值&b=2")
                    .putString("c", "我是通过put传递的参数")
                    .setOptions(opts)
                    .setRouteCallback { toast("MainActivity：路由跳转${it.resultStr}") }
                    .setActivityResultCallback {
                        if (it.resultCode == Activity.RESULT_OK) {
                            toast(it.data?.getStringExtra("result_key") ?: "没有获取到值")
                        }
                    }
                    .start(this)
            }.start()
        }

        binding.act2.setOnClickListener {
            Router
                .build("/activity/test2?cbd=True")
                .putInt("a", 6666)
                .setOptions(
                    ActivityOptionsCompat.makeCustomAnimation(
                        this,
                        R.anim.act_in,
                        R.anim.act_out
                    )
                )
                .setRouteCallback {
                    val str = it.getString("hold_key") ?: "hold没有返回数据"
                    toast("MainActivity：$str")
                }
                .setActivityResultCallback {
                    if (it.resultCode == Activity.RESULT_OK) {
                        toast(it.data?.getStringExtra("result_key") ?: "没有获取到值")
                    }
                }
                .start(this)
        }

        binding.act3.setOnClickListener {
            Router
                .build("/lib/act1")
                .setOptions(
                    ActivityOptionsCompat.makeCustomAnimation(
                        this,
                        R.anim.act_in,
                        R.anim.act_out
                    )
                )
                .start(this)
        }

        binding.act4.setOnClickListener {
            Router.build("/act/web")
                .putString("url","file:///android_asset/scheme-test.html")
                .start(this)
        }

        binding.hand1.setOnClickListener {
            Router
                .build("/handler/test1")
                .putInt("a", 1)
                .putInt("b", 2)
                .setRouteCallback {
                    val res = it.getInt("res")
                    toast("MainActivity：接收到计算结果$res")
                }
                .start(this)
        }

        binding.hand2.setOnClickListener {
            Router
                .build("/handler/test2")
                .putInt("a", 2)
                .putInt("b", 4)
                .setRouteCallback {
                    val res = it.getInt("res")
                    toast("MainActivity：接收到计算结果$res")
                }
                .start(this)
        }

        binding.hand3.setOnClickListener {
            Router
                .build("/lib/handler1")
                .start(this)
        }

        binding.interceptor1.setOnClickListener {
            Router
                .build("/activity/test1")
                //为了和第一个测试用例区分，增加一个字段标识让拦截器可以判断是否需要拦截
                .putBoolean("need_interrupt", true)
                .setRouteCallback { toast("MainActivity：路由跳转${it.resultStr}") }
                .setActivityResultCallback {
                    if (it.resultCode == Activity.RESULT_OK) {
                        toast(it.data?.getStringExtra("result_key") ?: "没有获取到值")
                    }
                }
                .start(this)
        }

        binding.interceptor3.setOnClickListener {
            Router
                .build("/activity/test3")
                .setRouteCallback { toast("MainActivity：路由跳转${it.resultStr}") }
                .setActivityResultCallback {
                    if (it.resultCode == Activity.RESULT_OK) {
                        toast(it.data?.getStringExtra("result_key") ?: "没有获取到值")
                    }
                }
                .start(this)
        }

        binding.interceptor4.setOnClickListener {
            Router
                .build("/activity/test4")
                .setRouteCallback { toast("MainActivity：路由跳转${it.resultStr}") }
                .setActivityResultCallback {
                    if (it.resultCode == Activity.RESULT_OK) {
                        toast(it.data?.getStringExtra("result_key") ?: "没有获取到值")
                    }
                }
                .start(this)
        }

        binding.interceptor5.setOnClickListener {
            Router
                .build("/handler/test4")
                .putInt("a", 2)
                .putInt("b", 4)
                .setRouteCallback {
                    if (it.resultCode != Result.SUCCEED) {
                        toast("MainActivity：路由跳转${it.resultStr}")
                    } else {
                        val res = it.getInt("res")
                        toast("MainActivity：接收到计算结果$res")
                    }
                }
                .start(this)
        }

        binding.any1.setOnClickListener {
            if (testService == null) {
                //实例化
                testService =
                    Router.anyLoader("/any/libservice/imp").newInstance(1, 3) as? TestService
            }
            testService?.test(this)
        }

    }

    private fun toast(msg: String) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
    }


}